<?php
class UserModel
{
	public static function GetUser()
	{
		include_once 'Zendvn/sql_connector.php';
		 
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$query = "select * from users";
		try {
			$result = $db->fetchAll($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetMaxIDUser(){
		include_once 'Zendvn/sql_connector.php';
		 
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		$query = "select Max(MenuID) from users";
		try {
			return $db->query($query);
		} catch (Zend_Db_Exception $e) {
			return 0;
		}
	}
	
	public static function GetNumUser()
	{
		include_once 'Zendvn/sql_connector.php';
		 
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$query = "select count(id) from users";
		try {
			$result = $db->fetchOne($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetIDFromName($name)
	{
		include_once 'Zendvn/sql_connector.php';
		
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");

		$query = "select id from users where name = '$name'";
		try {
			$result = $db->fetchOne($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetInfoFromID($id)
	{
		include_once 'Zendvn/sql_connector.php';
		
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");

		$query = "select * from users where id = '$id'";
		try {
			$result = $db->fetchRow($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function UpdateUser($id, $username, $password, $decentralize, $email, $fullname){
		include_once 'Zendvn/sql_connector.php';
		
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		 
		$where = "id = '$id'";
		$data = array(
				'username' => $username,
				'password' => $password,
				'decentralize' => $decentralize,
				'email' => $email,
				'full_name' => $fullname
		);
		try {
			$db->update('users', $data, $where);
		}
		catch (Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '. $e->getMessage();
		}
	}
	public static function DeleteUser($id){
		include_once 'Zendvn/sql_connector.php';
		
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		$where = "id = '$id'";
		try {
			$db->delete('users', $where);
			return TRUE;
		} catch (Zend_Db_Exception $e) {
			echo 'DB ERROR : '. $e->getMessage();
			return FALSE;
		}
	}
	
	public static function AddUser($id, $username, $password, $decentralize, $email, $fullname){
		include_once 'Zendvn/sql_connector.php';
		
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		//$id = GetMaxIDUser();
		//$sql = "INSERT INTO users(id, username, password, decentralize, email, fullname) VALUES($id, $username, $password, $decentralize, $email, $fullname)";
		$data = array(
				'id'=>$id,
				'username' => $username,
				'password' => $password,
				'decentralize' => $decentralize,
				'email' => $email,
				'full_name' => $fullname
		);
		try {
			$db->insert("users", $data);
			return TRUE;
		} catch (Zend_Db_Exception $e) {
			echo 'DB ERROR : '. $e->getMessage();
			return FALSE;
		}
	}
}